package projectFileIO;

import javax.swing.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JTextArea;

public class FileIOGUImain extends JApplet {
	private static JTextField txtDirections;
	private static JTextArea textArea;
	private JTextArea textAreaInput;
	static FileIOFunctions fileFunc = new FileIOFunctions();
	private String input;
	File file = new File("input.txt");

	public void init() {
		setSize(500, 300);
	}

	public FileIOGUImain() {
		getContentPane().setLayout(null);

		JPanel panel = new JPanel();
		panel.setBounds(10, 11, 480, 45);
		getContentPane().add(panel);
		panel.setLayout(null);

		txtDirections = new JTextField();
		txtDirections.setBounds(10, 11, 460, 23);
		txtDirections.setText("Type something in the left box... "
				+ "140 character max.");
		txtDirections.setEditable(false);
		panel.add(txtDirections);
		txtDirections.setColumns(50);
		

		JPanel panel_1 = new JPanel();
		panel_1.setBounds(250, 67, 240, 184);
		getContentPane().add(panel_1);
		panel_1.setLayout(null);

		JScrollPane outputScrollPane = new JScrollPane();
		outputScrollPane.setBounds(10, 11, 220, 162);
		panel_1.add(outputScrollPane);

		textArea = new JTextArea();
		outputScrollPane.setViewportView(textArea);
		textArea.setEditable(false);

		JPanel panel_2 = new JPanel();
		panel_2.setBounds(10, 67, 230, 184);
		getContentPane().add(panel_2);
		panel_2.setLayout(null);

		textAreaInput = new JTextArea();
		textAreaInput.setLineWrap(true);
		textAreaInput.setBounds(10, 11, 210, 162);
		panel_2.add(textAreaInput);

		JButton btnSaveToFile = new JButton("Save to File");
		btnSaveToFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				input = textAreaInput.getText();
				try {
					fileFunc.inputFile(file, input);
					fileFunc.copyFile();
				} catch (IOException e) {
					System.exit(0);
					updateDirections("File write error!");
				}

			}
		});
		btnSaveToFile.setBounds(70, 262, 118, 23);
		getContentPane().add(btnSaveToFile);

		JButton btnGetFromFile = new JButton("Get From File");
		btnGetFromFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					fileFunc.outputFile();
				} catch (IOException e) {
					updateDirections("Get file error!");
				}
			}
		});
		btnGetFromFile.setBounds(334, 262, 118, 23);
		getContentPane().add(btnGetFromFile);
	}

	public static void updateDirections(String string) {
		txtDirections.setText(string);
	}

	public static void updateJTextAreaOutput(String string) {
		textArea.append(string);
	}

	public static void eraseJTextAreaOutput() {
		textArea.setText("");
	}
	
}
